package com.study.goal.service.impl;

import cn.hutool.core.bean.BeanUtil;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.page.TableDataInfo;
import com.study.common.utils.StringUtils;
import com.study.goal.domain.UserIntegral;
import com.study.goal.domain.bo.UserIntegralBo;
import com.study.goal.domain.vo.UserIntegralVo;
import com.study.goal.mapper.UserIntegralMapper;
import com.study.goal.service.IUserIntegralService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户积分记录Service业务层处理
 *
 * @author MT
 * @date 2025-09-03
 */
@RequiredArgsConstructor
@Service
public class UserIntegralServiceImpl implements IUserIntegralService {

    private final UserIntegralMapper baseMapper;

    /**
     * 查询用户积分记录
     */
    @Override
    public UserIntegralVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户积分记录列表
     */
    @Override
    public TableDataInfo<UserIntegralVo> queryPageList(UserIntegralBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserIntegral> lqw = buildQueryWrapper(bo);
        Page<UserIntegralVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户积分记录列表
     */
    @Override
    public List<UserIntegralVo> queryList(UserIntegralBo bo) {
        LambdaQueryWrapper<UserIntegral> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserIntegral> buildQueryWrapper(UserIntegralBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserIntegral> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, UserIntegral::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getIntegralType()), UserIntegral::getIntegralType, bo.getIntegralType());
        lqw.eq(bo.getIntegralValue() != null, UserIntegral::getIntegralValue, bo.getIntegralValue());
        lqw.eq(bo.getBizId() != null, UserIntegral::getBizId, bo.getBizId());
        lqw.eq(StringUtils.isNotBlank(bo.getBizType()), UserIntegral::getBizType, bo.getBizType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), UserIntegral::getStatus, bo.getStatus());
        lqw.eq(bo.getExpireTime() != null, UserIntegral::getExpireTime, bo.getExpireTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUniqueKey()), UserIntegral::getUniqueKey, bo.getUniqueKey());
        lqw.eq(StringUtils.isNotBlank(bo.getFutureData()), UserIntegral::getFutureData, bo.getFutureData());
        return lqw;
    }

    /**
     * 新增用户积分记录
     */
    @Override
    public Boolean insertByBo(UserIntegralBo bo) {
        UserIntegral add = BeanUtil.toBean(bo, UserIntegral.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户积分记录
     */
    @Override
    public Boolean updateByBo(UserIntegralBo bo) {
        UserIntegral update = BeanUtil.toBean(bo, UserIntegral.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserIntegral entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户积分记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
